home *** CD-ROM | disk | FTP | other *** search
/ NeXTSTEP 3.1 (Developer) [x86] / NeXT Step 3.1 Intel dev.cdr.dmg / NextDeveloper / Examples / IndexingKit / Ledger / Transaction.m < prev    next >
Text File  |  1993-01-25  |  4KB  |  228 lines

  1. /*
  2.  * Main Transaction object - what is stored in the IXRecordManagers
  3.  *
  4.  * Author: Kris Younger, NeXT Systems Engineering
  5.  * You may freely copy, distribute and reuse the code in this example.  
  6.  * NeXT disclaims any warranty of any kind, expressed or implied, as to 
  7.  * its fitness for any particular use.
  8.  */
  9. /* Mostly Generated By SetGet - version 1.0 - kyounger */
  10.  
  11. #import "Transaction.h"
  12. #import <appkit/Application.h>
  13. #import "MainDelegate.h"
  14. #import    <regex.h>
  15.  
  16. @interface Transaction(PRIVATE)
  17. - (id)setTSerial:(int)tS;
  18. @end
  19.  
  20. @implementation Transaction
  21. - init
  22. {
  23.     char    number[16];
  24.  
  25.     tSerial = [[NXApp delegate] consumeSerial];
  26.     if (tSerial == 0) {
  27.     tSerial = -1;
  28.     };
  29.     tDate = [[NXApp delegate] todaysDate];
  30.     sprintf(number, "%d", tSerial);
  31.     tNumber = NXCopyStringBuffer(number);
  32.     return self;
  33. }
  34.  
  35. - free
  36. {
  37.     if (tNumber != NULL) { free(tNumber); tNumber = NULL; }
  38.     if (tMemo != NULL) { free(tMemo); tMemo = NULL; }
  39.     if (tDebit != NULL) { free(tDebit); tDebit = NULL; }
  40.     if (tCredit != NULL) { free(tCredit); tCredit = NULL; }
  41.     if (tBalance != NULL) { free(tBalance); tBalance = NULL; }
  42.     return[super free];
  43. }
  44.  
  45. - commit
  46. {
  47.     int                 rmHandle; /* not used*/
  48.     id                  storeHandle;
  49.  
  50.     if ((recordManager != NULL) && (recordManager != nil)) {
  51.     [(id)recordManager getBlock:&rmHandle andStore:&storeHandle];
  52.     [storeHandle startTransaction];
  53.     [recordManager replaceRecord:[self handle] with:self];
  54.     [storeHandle commitTransaction];
  55.     };
  56.     return self;
  57. }
  58.  
  59. - (unsigned int)handle
  60. {
  61.     return runTimeHandle;
  62. }
  63.  
  64. - (int)tSerial
  65. {
  66.     return ((int)tSerial);
  67. }
  68.  
  69. /* this should be a private method. */
  70. - (id)setTSerial:(int)tS
  71. {
  72.     tSerial = tS;
  73.     return self;
  74. }
  75.  
  76. - (const char *)tNumber
  77. {
  78.     return ((const char *)tNumber);
  79. }
  80.  
  81. - (id)setTNumber:(const char *)tN
  82. {
  83.     if (tNumber != NULL) free(tNumber);
  84.     tNumber = NXCopyStringBuffer(tN);
  85.     return self;
  86. }
  87.  
  88. - (const char *)tDate
  89. {
  90.     return ((const char *)tDate);
  91. }
  92.  
  93. - (id)setTDate:(const char *)tD
  94. {
  95.     tDate = NXUniqueString(tD);
  96.     return self;
  97. }
  98.  
  99. - (const char *)tMemo
  100. {
  101.     return ((const char *)tMemo);
  102. }
  103.  
  104. - (id)setTMemo:(const char *)tM
  105. {
  106.     if (tMemo != NULL) free(tMemo);
  107.     tMemo = NXCopyStringBuffer(tM);
  108.     return self;
  109. }
  110.  
  111. - (double)tAmount
  112. {
  113.     return ((double)tAmount);
  114. }
  115.  
  116. - (id)setTAmount:(double)tA
  117. {
  118.     char                buf[256];
  119.  
  120.     tAmount = tA;
  121.     if (tDebit != NULL) free(tDebit);
  122.     if (tCredit != NULL) free(tCredit);
  123.     if (tAmount >= 0) {
  124.     sprintf(buf, "%.2f", tAmount);
  125.     tCredit = NXCopyStringBuffer(buf);
  126.     tDebit = "";
  127.     } else {
  128.     sprintf(buf, "%.2f", -tAmount);
  129.     tDebit = NXCopyStringBuffer(buf);
  130.     tCredit = "";
  131.     };
  132.     return self;
  133. }
  134.  
  135. - (const char *)tBalance
  136. {
  137.     return (tBalance);
  138. }
  139.  
  140. - (id)setTBalance:(const char *)tB
  141. {
  142.     double              ttA;
  143.  
  144.     if (tBalance != NULL) free(tBalance);
  145.     tBalance = NXCopyStringBuffer(tB);
  146.     sscanf(tB, "%lf", &ttA);
  147.     tIBalance = ttA;
  148.     return self;
  149. }
  150.  
  151. - (const char *)tDebit
  152. {
  153.     return tDebit;
  154. }
  155.  
  156. - (id)setTDebit:(const char *)tD
  157. {
  158.     double              ttA;
  159.  
  160.     sscanf(tD, "%lf", &ttA);
  161.     ttA = -ttA;
  162.     [self setTAmount:ttA];
  163.     return self;
  164. }
  165.  
  166. - (const char *)tCredit
  167. {
  168.     return tCredit;
  169. }
  170.  
  171. - (id)setTCredit:(const char *)tC
  172. {
  173.     double              ttA;
  174.  
  175.     sscanf(tC, "%lf", &ttA);
  176.     [self setTAmount:ttA];
  177.     return self;
  178. }
  179.  
  180.  
  181. - source:aSource didReadRecord:(unsigned)record
  182. {
  183.     recordManager = aSource;
  184.     if (![recordManager isMemberOf:[IXRecordManager class]])
  185.     recordManager = [aSource delegate];
  186.  
  187.     runTimeHandle = record;
  188.     return self;
  189. }
  190.  
  191. - source:aSource willWriteRecord:(unsigned)record
  192. {
  193.     recordManager = nil;
  194.     if (runTimeHandle == 0) runTimeHandle = record;
  195.     return self;
  196. }
  197.  
  198. - (id)copy
  199. {
  200.     return[super copy];
  201. }
  202.  
  203. - (id)invertCopy
  204. {
  205.     id                  foo;
  206.  
  207.     foo = [super copy];
  208.     [foo setTAmount:-[foo tAmount]];
  209.     return foo;
  210. }
  211.  
  212. - (id)becomeVoided:(id)v
  213. {
  214.     char                buf[64];
  215.  
  216.     if (tNumber != NULL) free(tNumber);
  217.     tNumber = NXCopyStringBuffer("void");
  218.     [self setTDate:[[NXApp delegate] todaysDate]];
  219.     strcat(buf, "Void of #");
  220.     strcat(buf,[v tNumber]);
  221.     if (tMemo != NULL) free(tMemo);
  222.     tMemo = NXCopyStringBuffer(buf);
  223.     [self setTAmount:-[v tAmount]];
  224.     return self;
  225. }
  226.  
  227. @end
  228.